package com.company.genealogy.web.controller;

import com.company.genealogy.common.result.ApiResponse;
import com.company.genealogy.web.service.SpouseService;
import com.company.genealogy.web.vo.SpouseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 配偶信息控制器
 */
@RestController
@RequestMapping("/api/v1/spouse")
public class SpouseController {
    
    @Autowired
    private SpouseService spouseService;
    
    /**
     * 获取所有配偶信息（分页）
     */
    @GetMapping
    public ApiResponse<List<SpouseVO>> getSpouseList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            List<SpouseVO> spouses = spouseService.getSpouseList(page, size);
            return ApiResponse.success(spouses);
        } catch (Exception e) {
            return ApiResponse.fail("获取配偶信息列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID获取配偶信息
     */
    @GetMapping("/{id}")
    public ApiResponse<SpouseVO> getSpouseById(@PathVariable Long id) {
        try {
            SpouseVO spouse = spouseService.getSpouseById(id);
            if (spouse == null) {
                return ApiResponse.fail("配偶信息不存在");
            }
            return ApiResponse.success(spouse);
        } catch (Exception e) {
            return ApiResponse.fail("获取配偶信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 创建配偶信息
     */
    @PostMapping
    public ApiResponse<SpouseVO> createSpouse(@RequestBody SpouseVO spouseVO) {
        try {
            SpouseVO result = spouseService.createSpouse(spouseVO);
            return ApiResponse.success(result);
        } catch (Exception e) {
            return ApiResponse.fail("创建配偶信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新配偶信息
     */
    @PutMapping("/{id}")
    public ApiResponse<SpouseVO> updateSpouse(@PathVariable Long id, @RequestBody SpouseVO spouseVO) {
        try {
            SpouseVO result = spouseService.updateSpouse(id, spouseVO);
            return ApiResponse.success(result);
        } catch (Exception e) {
            return ApiResponse.fail("更新配偶信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除配偶信息
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Boolean> deleteSpouse(@PathVariable Long id) {
        try {
            boolean result = spouseService.deleteSpouse(id);
            if (result) {
                return ApiResponse.success(true);
            } else {
                return ApiResponse.fail("删除配偶信息失败");
            }
        } catch (Exception e) {
            return ApiResponse.fail("删除配偶信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据姓名搜索配偶
     */
    @GetMapping("/search")
    public ApiResponse<List<SpouseVO>> searchSpouseByName(@RequestParam String name) {
        try {
            List<SpouseVO> spouses = spouseService.searchSpouseByName(name);
            return ApiResponse.success(spouses);
        } catch (Exception e) {
            return ApiResponse.fail("搜索配偶信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据性别查询配偶
     */
    @GetMapping("/gender/{gender}")
    public ApiResponse<List<SpouseVO>> getSpouseByGender(@PathVariable String gender) {
        try {
            List<SpouseVO> spouses = spouseService.getSpouseByGender(gender);
            return ApiResponse.success(spouses);
        } catch (Exception e) {
            return ApiResponse.fail("查询配偶信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据状态查询配偶
     */
    @GetMapping("/status/{status}")
    public ApiResponse<List<SpouseVO>> getSpouseByStatus(@PathVariable String status) {
        try {
            List<SpouseVO> spouses = spouseService.getSpouseByStatus(status);
            return ApiResponse.success(spouses);
        } catch (Exception e) {
            return ApiResponse.fail("查询配偶信息失败：" + e.getMessage());
        }
    }
} 